<?php
  	$title ="Log In";
    include_once("includes/init.php");
    $noSeasonChange = true;
    $submit = getParam("submit");
    if(isset($loggedin))
    {
        if (isset($_GET["out"]))
        {
            session_unregister("loggedin");
			setcookie("coach", "", 0, $rootDir);
            include("includes/redirect.php");
            return;
        }
        else
        {
            include_once("includes/header.php");
echo <<<EOQ
<tr><td align="center" colspan="2"><h2>You are already logged in $loggedin->name</h2></td</tr>
<tr><td align="center" colspan="2">Choose a link on the left or <a href="login.php?out=true">click here to log out</a></td</tr>
EOQ;
        }
    }
    else if(!isset($submit))
    {
        include_once("includes/header.php");
        include("includes/login_form.php");
    }
    else
    {
        $coach = loginCoach(getParam("username"), getParam("pword"));
        if (false == $coach)
        {
            include_once("includes/header.php");
echo <<<EOQ
<tr><td align="center" colspan="2">Sorry that login is incorrect, please try again</td</tr>
EOQ;
            include("includes/login_form.php");
        }
        else
        {
            $loggedin = $coach;
			setcookie("coach", $coach->id, time() + 157248000, $rootDir);
            $_SESSION["loggedin"] = $coach;
            $message = "Welcome back $loggedin->name";
            $redirect="coach_details.php?id=$loggedin->id";
            include("includes/redirect.php");
            return;
        }
  }
  include("includes/footer.php");
?>
